package com.datas.utils;

import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局的controller 配置增强
 */
@ControllerAdvice
public class GlobeControllerAdvice {

    /**
     * 捕获requestmapping的错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.EXPECTATION_FAILED)
    @ResponseBody
    public String processErr(Exception e) {
        LoggerFactory.getLogger(getClass()).error("发生一个错误: " + e, e);
//        e.printStackTrace();
        return new JSONObject().put("error", 1).toString();
    }
}
